package com.example.fan.airline_reservation;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.fan.airline_reservation.Model.USER;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

public class Sign_in extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        /*getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }*/
        Bmob.initialize(this, "0802cbf346296dc9fac5a477f911ba39");
        final Button login=(Button)findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });

        final Button register=(Button)findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toRegister();
            }
        });

    }

    public void login(){
        EditText name = (EditText)findViewById(R.id.username);
        final String myname = name.getText().toString();
        EditText password = (EditText)findViewById(R.id.userpassname);
        final String mypassword = password.getText().toString();

        if (myname.length()!=0 && mypassword.length()!=0) {
            String bq = "select * from USER where name = '" + myname + "'";
            /*String bq ="select * from USER ";*/
            /*Log.d("test",bq);*/
            new BmobQuery<USER>().doSQLQuery(bq, new SQLQueryListener<USER>() {
                @Override
                public void done(BmobQueryResult<USER> result, BmobException e) {
                    if (e == null) {
                        List<USER> list = (List<USER>) result.getResults();
                        if (list != null && list.size() > 0) {
                            if (list.get(0).getPassword().equals(mypassword)) {
                                //ToastShow("登录成功！");
                                if (list.get(0).getType() == 1) {
                                    Intent intent = new Intent(Sign_in.this, AC_Interface.class);

                                    startActivity(intent);
                                } else if (list.get(0).getType() == 0) {
                                    Intent intent = new Intent(Sign_in.this, Traveler_Interface.class);
                                    USER user = list.get(0);
                                    Log.e("TestIntentSI", user.getName());
                                    intent.putExtra("USER11", user);
                                    startActivity(intent);
                                }
                                finish();
                            } else {
                                ToastShow("密码错误！");
                            }
                        } else {
                            ToastShow("暂时还没有您的信息，请您先进行注册");
                        }
                    } else {
                        ToastShow("错误码：" + e.getErrorCode() + "，错误描述：" + e.getMessage());
                    }
                }
            });
        }else {
            ToastShow("用户名或密码不能为空！");
        }
    }

    public void toRegister(){
        Intent intent = new Intent(Sign_in.this,register.class);
        startActivity(intent);
        finish();
    }

    public void ToastShow(String message){
        Toast.makeText(Sign_in.this,message,Toast.LENGTH_SHORT).show();
    }

}
